﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace QLNSClient
{
    public partial class Frm_LoaiPCKN : DevExpress.XtraEditors.XtraForm
    {
        QLNSServiceReference.QLNSServiceClient sv = new QLNSServiceReference.QLNSServiceClient();
        
        public Frm_LoaiPCKN()
        {
            InitializeComponent();
        }

        public void LoadData()
        {
            btn_Luu.Enabled = false;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;
            btn_Huy.Enabled = false;

            txt_MaLoaiPCKN.Enabled = false;
            txt_TenLPCKN.Enabled = false;
            
            gv_dsLPCKN.DataSource = sv.getLPCKN();
            gridView1.Columns[0].Caption = "Mã loại";
            gridView1.Columns[1].Caption = "Tên loại";
        }

        private void Frm_LoaiPCKN_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = true;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;
            btn_Huy.Enabled = true;

            txt_MaLoaiPCKN.Enabled = false;
            txt_TenLPCKN.Enabled = true;

            txt_TenLPCKN.Text = "";

            List<QLNSServiceReference.LOAI_PCKN> listLastID = new List<QLNSServiceReference.LOAI_PCKN>(sv.getLastLPCKN());
            int lastID = listLastID[0].Ma_LoaiPCKN;
            txt_MaLoaiPCKN.Text = (lastID + 1).ToString();
        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            btn_Huy.Enabled = false;
            btn_Them.Enabled = true;

            string tempMaLPCKN = txt_MaLoaiPCKN.Text;
            string tempTenLPCKN = txt_TenLPCKN.Text;

            QLNSServiceReference.LOAI_PCKN lpckn = new QLNSServiceReference.LOAI_PCKN();
            lpckn.Ma_LoaiPCKN = Int32.Parse(tempMaLPCKN);
            lpckn.TenLPCKN = tempTenLPCKN;

            if (demsua == 1)
            {
                sv.updateLPCKN(lpckn);
                LoadData();
                demsua = 0;
                MessageBox.Show("Sửa thành công!", "Thông báo");
            }
            else
            {
                int kq = sv.addLPCKN(lpckn);
                if (kq != -1)
                {
                    LoadData();
                    MessageBox.Show("Lưu thành công!", "Thông báo");
                }
                else
                {
                    MessageBox.Show("Dữ liệu đã tồn tại vui lòng xem lại!", "Cảnh báo");
                }
            }
        }

        private void btn_Huy_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = false;
            btn_Huy.Enabled = false;
            btn_Them.Enabled = true;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;
        }

        int demsua = 0;
        private void btn_Sua_Click(object sender, EventArgs e)
        {
            demsua = 1;

            btn_Luu.Enabled = true;
            btn_Them.Enabled = false;
            btn_Huy.Enabled = true;
            btn_Sua.Enabled = false;

            txt_TenLPCKN.Enabled = true;
            txt_TenLPCKN.Text = "";
        }

        private void btn_Xoa_Click(object sender, EventArgs e)
        {
            btn_Them.Enabled = true;
            btn_Luu.Enabled = false;
        }

        private void gv_dsLPCKN_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = false;
            btn_Xoa.Enabled = true;
            btn_Sua.Enabled = true;
            btn_Huy.Enabled = false;
            btn_Them.Enabled = true;

            txt_MaLoaiPCKN.Enabled = false;
            txt_TenLPCKN.Enabled = false;
        }
    }
}